package com.itic.appbase.framework.utils.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itic.appbase.framework.utils.StringHelper;

public class MixedConfiguration
        implements Configuration {

    private static final Logger logger = LoggerFactory.getLogger(MixedConfiguration.class);

    private static final String PREFIX_FILE = "file:";

    private static final String EMPTY = "";

    private static final String PREFIX_CLASSPATH = "classpath:";

    private Configuration[] configurations;

    public MixedConfiguration(String[] configPropertiesFiles) {
        initConfigurations(configPropertiesFiles);
    }

    private void initConfigurations(String[] configPropertiesFiles) {
        if (configPropertiesFiles == null || configPropertiesFiles.length == 0) {
            return;
        }
        configurations = new Configuration[configPropertiesFiles.length];
        for (int i = 0; i < configurations.length; i++) {
            logger.info("loading properties file: {}", configPropertiesFiles[i]);
            if (configPropertiesFiles[i].startsWith(PREFIX_FILE)) {
                configurations[i] = new FilePropertiesConfiguration(StringHelper.substringAfter(
                        configPropertiesFiles[i], PREFIX_FILE));
            } else if (configPropertiesFiles[i].startsWith(PREFIX_CLASSPATH)) {
                configurations[i] = new ClassPathPropertiesConfiguration(StringHelper.substringAfter(
                        configPropertiesFiles[i], PREFIX_CLASSPATH));
            }
        }
    }

    @Override
    public String getString(String key) {
        for (Configuration configuration : configurations) {
            if (configuration.containsKey(key)) {
                return configuration.getString(key);
            }
        }
        return EMPTY;
    }

    @Override
    public boolean getBoolean(String key) {
        for (Configuration cfg : configurations) {
            if (cfg.containsKey(key)) {
                return cfg.getBoolean(key);
            }
        }
        return false;
    }

    @Override
    public int getInt(String key) {
        for (Configuration cfg : configurations) {
            if (cfg.containsKey(key)) {
                return cfg.getInt(key);
            }
        }
        return -1;
    }

    @Override
    public String[] getStringArray(String key) {
        for (Configuration cfg : configurations) {
            if (cfg.containsKey(key)) {
                return cfg.getStringArray(key);
            }
        }
        return new String[0];
    }

    @Override
    public long getLong(String key) {
        for (Configuration cfg : configurations) {
            if (cfg.containsKey(key)) {
                return cfg.getLong(key);
            }
        }
        return -1L;
    }

    @Override
    public float getFloat(String key) {
        for (Configuration cfg : configurations) {
            if (cfg.containsKey(key)) {
                return cfg.getFloat(key);
            }
        }
        return -1F;
    }

    @Override
    public double getDouble(String key) {
        for (Configuration cfg : configurations) {
            if (cfg.containsKey(key)) {
                return cfg.getDouble(key);
            }
        }
        return 0;
    }

    @Override
    public boolean containsKey(String key) {
        for (Configuration cfg : configurations) {
            if (cfg.containsKey(key)) {
                return true;
            }
        }
        return false;
    }
}
